package com.taoyibao.hongcanting.ui.home.views;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.BaseRelativeLayout;
import com.taoyibao.hongcanting.model.MakeUpModel;
import com.taoyibao.hongcanting.model.TasteModel;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author TangAnna
 * @description: 选择口味的一级Item
 * @date :${DATA} 19:16
 */
public class ItemChooseAddArea extends BaseRelativeLayout<List<TasteModel>> {

    private TagFlowLayout mTagFlowLayout;
    private LayoutInflater mInflater;
    private TextView mTvName;
    private List<MakeUpModel> mMakeUpModels;
    private List<TasteModel> mTasteModels;


    public ItemChooseAddArea(Context context) {
        super(context);
    }

    public ItemChooseAddArea(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemChooseAddArea(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        mTagFlowLayout = findViewById(R.id.tfl_list);
        mTvName = findViewById(R.id.tv_choose_add_name);
        mInflater = LayoutInflater.from(getContext());
        mMakeUpModels = new ArrayList<>();
        mTasteModels = new ArrayList<>();
    }

    /**
     * 设置口味数据
     *
     * @param data
     */
    @Override
    public void setData(List<TasteModel> data) {
        mTasteModels.addAll(data);
        mTagFlowLayout.setAdapter(new TagAdapter<TasteModel>(mTasteModels) {
            @Override
            public View getView(FlowLayout parent, int position, TasteModel tasteModel) {
                TextView textView = (TextView) mInflater.inflate(R.layout.item_choose_add_secondary, null);
                textView.setText(tasteModel.taste_name);
                return textView;
            }


            /**设置默认显示被选择的数据
             * @param position
             * @param tasteModel
             * @return
             */
            @Override
            public boolean setSelected(int position, TasteModel tasteModel) {
                return position == 0;
            }
        });
    }

    /**
     * 设置做法数据
     *
     * @param data
     */
    public void setMakeUpData(List<MakeUpModel> data) {
        mMakeUpModels.addAll(data);
        mTagFlowLayout.setAdapter(new TagAdapter<MakeUpModel>(mMakeUpModels) {
            @Override
            public View getView(FlowLayout parent, int position, MakeUpModel makeUpModel) {
                TextView textView = (TextView) mInflater.inflate(R.layout.item_choose_add_secondary, null);
                textView.setText(makeUpModel.practice_name);
                return textView;
            }

            /**设置默认选择的数据
             * @param position
             * @param makeUpModel
             * @return
             */
            @Override
            public boolean setSelected(int position, MakeUpModel makeUpModel) {
                return position == 0;
            }
        });
    }

    /**
     * 获取选择的口味
     */
    public TasteModel getSelectTaste() {
        Set<Integer> selectedList = mTagFlowLayout.getSelectedList();
        if (selectedList != null && selectedList.size() > 0) {
            Iterator<Integer> iterator = selectedList.iterator();
            if (iterator.hasNext()) {
                Integer next = iterator.next();
                return mTasteModels.get(next);
            }
        }
        return null;
    }

    /**
     * 获取选择的做法
     */
    public MakeUpModel getSelectMakeUp() {
        Set<Integer> selectedList = mTagFlowLayout.getSelectedList();
        if (selectedList != null && selectedList.size() > 0) {
            Iterator<Integer> iterator = selectedList.iterator();
            if (iterator.hasNext()) {
                Integer next = iterator.next();
                return mMakeUpModels.get(next);
            }
        }
        return null;
    }

    /**
     * 设置添加的分类名称
     *
     * @param addName
     */
    public void setChooseAddName(String addName) {
        if (!TextUtils.isEmpty(addName)) {
            mTvName.setText(addName);
        }
    }

    @Override
    protected int bindLayout() {
        return R.layout.item_choose_add;
    }
}
